﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Weight.Base;

namespace Weight.Controls
{
    public class LYFMenuButton : Label
    {
        public LYFMenuButton()
        {
            this.DoubleBuffered = true;//设置本窗体
            this.SetStyle(
                      //ControlStyles.UserPaint |    //使用自定义的绘制方式
                      ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                    | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                    | ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                    | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                    | ControlStyles.DoubleBuffer
                    , true);
            this.Dock = DockStyle.Top;
            this.Height = 30;
            this.AutoSize = false;
            this.BackColor = Color.FromArgb(0, 163, 129);
            this.Cursor = Cursors.Hand;
            _image = Properties.Resources.Menu1;
            _expandImage = Properties.Resources.Expand_Bottom;

            expandRect = new Rectangle(this.Width - 25, (this.Height - 9) / 2, 9, 9);
        }

        #region 属性
        [System.ComponentModel.Browsable(true)]
        public int Index { get; set; }

        private Color _backColor;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Color), "")]
        public new Color BackColor
        {
            get => _backColor;
            set
            {
                _backColor = value;
                this.Invalidate();
            }
        }

        private Image _image;
        [System.ComponentModel.Browsable(true)]
        public new Image Image { get => _image; set => _image = value; }

        private Image _expandImage;
        [System.ComponentModel.Browsable(true)]
        public Image ExpandImage { get => _expandImage; set => _expandImage = value; }

        private string _text;
        public new string Text
        {
            get => _text;
            set
            {
                _text = value;
                this.Invalidate();
            }
        }

        private bool _expanded;
        [System.ComponentModel.Browsable(true)]
        public bool Expanded { get => _expanded; set => _expanded = value; }

        private int _level;
        [System.ComponentModel.Browsable(true)]
        public int Level
        {
            get => _level;
            set
            {
                _level = value;
                //if (_level == 0)
                //    _image = Properties.Resources.Menu2;
                //else if (_level == 0)
                //    _image = Properties.Resources.Menu1;
            }
        }

        private bool _isFocus;
        public bool IsFocus
        {
            get => _isFocus;
            set
            {
                _isFocus = value;
                if (!_isFocus)
                    mouseEnterStatus = MouseStatus.None;
                else
                    mouseEnterStatus = MouseStatus.Enter;
                this.Invalidate();
            }
        }


        public Font TitleFont
        {
            get => new Font(this.Font.FontFamily, this.Font.Size + 1, FontStyle.Regular);
        }
        #endregion

        #region 事件
        public event EventHandler<ClickEventArgs> RootClick;
        public event EventHandler<ClickEventArgs> ChildClick;
        public event EventHandler<ClickEventArgs> ExpandedChanged;
        #endregion

        #region 自定义
        private Rectangle expandRect;
        private MouseStatus mouseEnterStatus = MouseStatus.None;
        #endregion

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}

        protected override void OnPaint(PaintEventArgs e)
        {
            if (_level == 0)
            {
                if (mouseEnterStatus == MouseStatus.Enter)
                    e.Graphics.FillRectangle(new SolidBrush(Colors.ColorChangedLightColour(_backColor, -0.3f)), e.ClipRectangle);
                else if (mouseEnterStatus == MouseStatus.Click)
                    e.Graphics.FillRectangle(new SolidBrush(Colors.ColorChangedLightColour(_backColor, -0.3f)), e.ClipRectangle);
                else
                    e.Graphics.FillRectangle(new SolidBrush(_backColor), e.ClipRectangle);

                if (Image != null)
                    e.Graphics.DrawImage(Image, new Rectangle((int)(this.Height * 0.2),
                                                                   (int)(this.Height * 0.2),
                                                                   (int)(this.Height * 0.6),
                                                                   (int)(this.Height * 0.6)));
                if (_expanded)
                    e.Graphics.DrawImage(Properties.Resources.Expand_Bottom, expandRect);
                else
                    e.Graphics.DrawImage(Properties.Resources.Expand_Left, expandRect);

                if (_text != null)
                    e.Graphics.DrawString(_text, this.TitleFont, new SolidBrush(this.ForeColor), new Point(this.Height, (this.Height - this.Font.Height) / 2));
            }
            else if (_level == 1)
            {
                if (mouseEnterStatus == MouseStatus.Enter)
                    e.Graphics.FillRectangle(new SolidBrush(Colors.ColorChangedLightColour(_backColor, 0.3f)), e.ClipRectangle);
                else if (mouseEnterStatus == MouseStatus.Click)
                    e.Graphics.FillRectangle(new SolidBrush(Colors.ColorChangedLightColour(_backColor, 0.3f)), e.ClipRectangle);
                else
                    e.Graphics.FillRectangle(new SolidBrush(_backColor), e.ClipRectangle);

                if (Image != null)
                    e.Graphics.DrawImage(Image, new Rectangle((int)(this.Height * 0.6),
                                                                   (int)(this.Height * 0.2),
                                                                   (int)(this.Height * 0.6),
                                                                   (int)(this.Height * 0.6)));
                if (_text != null)
                    e.Graphics.DrawString(_text, this.Font, new SolidBrush(this.ForeColor), new Point(this.Height + (int)(this.Height * 0.6), (this.Height - this.Font.Height) / 2));
            }


            base.OnPaint(e);
        }

        public  void PerformMouseClick()
        {
            OnMouseClick(null);
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            _expanded = !_expanded;
            mouseEnterStatus = MouseStatus.Click;
            _isFocus = true;
            this.Focus();



            if (Level == 0)
                RootClick?.Invoke(this, new ClickEventArgs(Index, Expanded) { BackColor = BackColor });
            else if (Level == 1)
                ChildClick?.Invoke(this, new ClickEventArgs(Index, Expanded) { BackColor = BackColor, Name = this.Name, Text = this.Text });
            base.OnMouseClick(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            if (!IsFocus)
                mouseEnterStatus = MouseStatus.Enter;
            this.Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (!IsFocus)
                mouseEnterStatus = MouseStatus.Leave;
            this.Invalidate();
            base.OnMouseLeave(e);
        }


        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.Height < 30)
                this.Height = 30;
            expandRect = new Rectangle(this.Width - 25, (this.Height - 9) / 2, 9, 9);
            base.OnSizeChanged(e);
        }

        public void SetSkin(Color color)
        {
            this.BackColor = color;
        }

        private enum MouseStatus
        {
            None,
            Enter,
            Leave,
            Click,
        }

        public class ClickEventArgs : EventArgs
        {
            public ClickEventArgs(int index, bool expanded)
            {
                Index = index;
                Expanded = expanded;
            }
            public string Name { get; set; }
            public string Text { get; set; }
            public int Index { get; set; }

            public bool Expanded { get; set; }
            public Color BackColor { get; set; }
        }
    }
}
